package com.rambler.crawler.spider;

import com.rambler.api.crawler.common.SpiderTask;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

@Component
@Slf4j
public class WeatherSpider implements SpiderTask {

    private static final String API_KEY = "YOUR_API_KEY"; // 请替换为您的实际 API Key
    private static final String BASE_URL = "https://api.weatherapi.com/v1/current.json";

    @Override
    public String getSpiderName() {
        return "WeatherSpiderService";
    }

    @Override
    public boolean supports(String type) {
        return "weather".equalsIgnoreCase(type);
    }

    @Override
    public void execute() {
        String location = "Beijing"; // 您想查询的地点
        String url = String.format("%s?key=%s&q=%s", BASE_URL, API_KEY, location);

        RestTemplate restTemplate = new RestTemplate();
        try {
            ResponseEntity<String> response = restTemplate.getForEntity(url, String.class);
            System.out.println("天气数据：" + response.getBody());
        } catch (Exception e) {
            System.err.println("获取天气数据失败：" + e.getMessage());
        }
    }
}
